<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="queryParams">
				<el-form-item>
					<el-input v-model="queryParams.keyword" placeholder="经理搜索"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="queryKeyWord">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageParams.dictionaryTypes" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
				 style="width: 100%;height: 420px" >
			<el-table-column type="selection" width="150">
			</el-table-column>
			<el-table-column type="index" width="150">
			</el-table-column>
			
			<el-table-column prop="name" label="明细名称" min-width="200" sortable>
			</el-table-column>
			<el-table-column prop="systemDictionaryType.name" label="明细所属类型" min-width="200" sortable>
			</el-table-column>
			<el-table-column label="操作" width="">
				<template scope="scope">
					<el-button size="large" type="success" round @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" round size="large" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="queryParams.pageSize" :total="pageParams.totals" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="true">
			<el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
				<el-form-item label="字典明细名称" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="字典明细类型" prop="systemDictionaryType">
					<template>
						<el-select v-model="editForm.systemDictionaryType" value-key="id" placeholder="请选择" >
							<el-option
									v-for="ts in types"
									:label="ts.name"
									:value="ts">
								<span style="float: left">{{ ts.name }}</span>
								<span style="float: right; color: #8492a6; font-size: 13px">{{ ts.id }}</span>
							</el-option>
						</el-select>
					</template>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>



	</section>
</template>

<script>
	import util from '../../../src/common/js/util'
	//import NProgress from 'nprogress'
	import {
		getSystemDictionaryDetailListPage,
		removeSystemDictionaryDetail,
		batchRemoveSystemDictionaryDetail,
		editSystemDictionaryDetail,
		getAllSystemDictionaryTypes,
		getALLShopNoMan
	} from '../../../src/api/api';

	export default {
		data() {
			return {

				pageParams : { // 接受结果集
					dictionaryTypes: [], // 接收数据列
					totals: 0, // 查询出来的总条数
				},
				title : "",
				queryParams : { // 封装 查询条件
					currentPage: 1, // 默认当前页面1
					pageSize: 5, // 默认 显示条数
					keyword: '' // 高级查询的 查询参数
				},
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false, // 盲等框
				editFormRules: { //
					name: [
						{ required: true, message: '请输入数据字典名称', trigger: 'blur' } // 必填，失去焦点触发
					],
					systemDictionaryType : [
						{ required: true, message: '必选选择类型', trigger: ['blur', 'change'] }
					]
				},
				//编辑界面数据
				editForm: {
					id : null,
					name : null,
					types_id : null

				},
				types:[] // 装类型的数据

			}
		},
		methods: {
			//状态显示转换
			formatStates: function (row, column) {

				return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知';
			},
			handleCurrentChange(val) {
				this.queryParams.currentPage = val;
				this.getSystemDictionaryDetails();
			},
			//获取用户列表
			getSystemDictionaryDetails() {
				this.listLoading = true; // 显示 忙碌框
				getSystemDictionaryDetailListPage(this.queryParams).then((res) => {
					// 给绑定的数据赋值
					this.pageParams.totals = res.data.totals;
					this.pageParams.dictionaryTypes = res.data.data;
					this.listLoading = false;
				}).catch(res => {
					this.$message.error("系统繁忙，请稍后重试")
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					removeSystemDictionaryDetail(row.id).then((res) => {
						this.listLoading = false; // 关闭盲等框
						if (res.data.success) {
							this.countPageSize(index);
							this.$message({  // 成功之后回调，弹框
								message: '删除成功',
								type: 'success'
							});
							this.getSystemDictionaryDetails(); // 重新加载数据
						} else { // 500错误
							this.$message.error(res.data.msg)
						}
					}).catch(res => {
						this.$message.error("系统繁忙，请稍后重试")  // 连不上服务端代码，或者404 就会调用
					});
				}).catch(() => {
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true; // 显示弹框
				this.title = "编辑经理";
				this.getAllTypes(); // 显示所有分类
				this.editForm = Object.assign({}, row); // 数据绑定


			},
			//显示新增界面
			handleAdd: function () {
				this.editFormVisible = true;
				this.title = "添加经理";
				this.getAllTypes(); // 显示所有分类
				this.editForm = {  // 初始化数据
				};
			},
			// 修改或者增加提交数据
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							editSystemDictionaryDetail(this.editForm).then((res) => {
								if (res.data.success) {
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['editForm'].resetFields();
									this.getSystemDictionaryDetails();
								} else {
									this.$message({
										message: res.data.msg,
										type: 'error'
									});
								}
								this.editLoading = false; // 关闭盲等框
								this.editFormVisible = false;
							}).catch(() => {
								this.$message.error('系统错误,请稍后重试');
								this.editLoading = false;// 关闭盲等框
							});
						})
					}
				})
			},
			// 页面跳转改变当前页码
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true; // 显示忙等框
					batchRemoveSystemDictionaryDetail(ids).then((res) => {
						this.listLoading = false; // 关闭
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getSystemDictionaryDetails();
					});
				}).catch(() => {
					this.$message.error("系统繁忙，请稍后重试")  // 连不上服务端代码，或者404 就会调用
				});
			},
			// 高级搜索
			queryKeyWord() { // 当使用高级搜索时，将搜索的当前页码 重置为1
				this.queryParams.currentPage = 1;
				this.getSystemDictionaryDetails();
			},
			// 删除时，判断是否时最后一页的唯一一条
			countPageSize(index) {
				let cPage = this.queryParams.currentPage;
				let pSize = this.queryParams.pageSize;
				let total = this.pageParams.totals;
				// 计算是否是最后一条
				if ( (cPage - 1) * pSize + index == total - 1  && index == 0) {
					this.queryParams.currentPage  = this.queryParams.currentPage - 1;
				}
			},
			getAllTypes() {
				getAllSystemDictionaryTypes().then(res => {
					this.types = res.data.data;
				}).catch(()=> {
					this.$message.error('获取分类信息失败,请稍后重试');
				})
			},

		},
		mounted() {
			this.getSystemDictionaryDetails(); // 当页面加载时请数据
		}
	}

</script>

<style scoped>

</style>